package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.internetofthings.firefightingiot;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class PictureDownload {
    public static void main(String[] args) {
        String url = "https://link-vision-picture-sh.oss-cn-shanghai.aliyuncs.com/FwuN4_jgak0x0U_Q6OKnxJcujqDNtAaGeLXtDZQDktg/1ec042be5be047c7a1a4ecadedd10eb7?Expires=1599626228&OSSAccessKeyId=LTAILduaCDAC561K&Signature=XS3ZQie%2F%2BpWy2zT%2BUY4DGzz0CqI%3D";
        downloadPicture(url);
    }
    //链接url下载图片
    private static void downloadPicture(String urlList) {
        URL url = null;
        int imageNumber = 0;

        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            String imageName =  "E:/test.jpg";

            FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context=output.toByteArray();
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
